package huahua.pm25.controller;

import huahua.pm25.pojo.Pm25;
import huahua.pm25.service.Impl.Pm25ServiceImpl;
import huahua.pm25.service.Pm25Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: zhanghuadi
 * @date: 2018/4/27 0027
 * @description: pm2.5数据操作接口
 */
@RestController
public class Pm25Controller {

    private final Integer MAX_PM_VALUE = 1000;

    @Autowired
    private Pm25ServiceImpl pm25Service;

    /**
     * 上传数据接口
     * @param value
     */
    @RequestMapping(value = "/pm25/value",method = RequestMethod.POST)
    public void pm25(@RequestParam("value") String value){
        //value数值处理
        Integer pmValue = Integer.parseInt(value);
        System.out.println(pmValue);
        if (pmValue>= 0 && pmValue< MAX_PM_VALUE){
            pm25Service.addValue(pmValue);
        }
    }

    /**
     * 查询指定时间段数据
     */
    @RequestMapping(value = "/pm25/periodValues")
    public List<Pm25> periodValues(@RequestParam("startTime") String start,
                                   @RequestParam("endTime") String end) throws ParseException {
        Date startTime, endTime;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        startTime = dateFormat.parse(start);
        endTime = dateFormat.parse(end);

        if (startTime.equals(null)){
            startTime = dateFormat.parse("2018-04-27 23:31:31");
        }else if(endTime.equals(null)){
            endTime = new Date();
        }else if (startTime.compareTo(endTime) > 0){

        }
        return pm25Service.selectValue(startTime, endTime);
    }
}
